package com.it135.citytraffic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.it135.citytraffic.pojo.dto.TrafficInfoAverageDTO;
import com.it135.citytraffic.pojo.dto.TrafficInfoPageQueryDTO;
import com.it135.citytraffic.pojo.entity.TrafficInfo;
import com.it135.citytraffic.mapper.TrafficInfoMapper;
import com.it135.citytraffic.pojo.result.PageResult;
import com.it135.citytraffic.pojo.vo.TrafficInfoAverageVO;
import com.it135.citytraffic.pojo.vo.TrafficInfoVO;
import com.it135.citytraffic.service.ITrafficInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.it135.citytraffic.utils.BeanCopyTool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;


@Service
@Slf4j
public class TrafficInfoServiceImpl extends ServiceImpl<TrafficInfoMapper, TrafficInfo> implements ITrafficInfoService {
    @Autowired
    private TrafficInfoMapper trafficInfoMapper;

    /**
     * 分页查询
     *
     * @param trafficInfoPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(TrafficInfoPageQueryDTO trafficInfoPageQueryDTO) {
        Integer status = trafficInfoPageQueryDTO.getStatus();
        String name = trafficInfoPageQueryDTO.getName();
        String description = trafficInfoPageQueryDTO.getDescription();
        LocalDateTime beginTime = trafficInfoPageQueryDTO.getBeginTime();
        LocalDateTime endTime = trafficInfoPageQueryDTO.getEndTime();
        Integer start = null;
        Integer end = null;
        if (trafficInfoPageQueryDTO.getBlocked() != null && !trafficInfoPageQueryDTO.getBlocked().equals("")) {
            String[] split = trafficInfoPageQueryDTO.getBlocked().split("-");
            start = Integer.valueOf(split[0]);
            end = Integer.valueOf(split[1]);
        }
        LambdaQueryWrapper<TrafficInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(status != null, TrafficInfo::getStatus, status)
                .like(name != null && !name.equals(""), TrafficInfo::getName, name)
                .like(description != null && !description.equals(""), TrafficInfo::getDescription, description)
                .gt(beginTime != null && !beginTime.equals(""), TrafficInfo::getCreateTime, beginTime)
                .lt(endTime != null && !endTime.equals(""), TrafficInfo::getCreateTime, endTime)
                .apply(start != null, "CAST(REPLACE(blocked, '%', '') AS DECIMAL) > {0}", start)
                .apply(end != null, "CAST(REPLACE(blocked, '%', '') AS DECIMAL) < {0}", end);
        Page<TrafficInfo> page = new Page<>(trafficInfoPageQueryDTO.getPage(), trafficInfoPageQueryDTO.getPageSize());
        Page<TrafficInfo> trafficInfoPage = trafficInfoMapper.selectPage(page, queryWrapper);
        List<TrafficInfoVO> trafficInfoVOS = BeanCopyTool.convertCopyList(trafficInfoPage.getRecords(), TrafficInfoVO.class);
        return new PageResult(trafficInfoPage.getTotal(), trafficInfoVOS);
    }

    /**
     * 获取平均
     *
     * @param trafficInfoAverageDTO
     * @return
     */
    @Override
    public TrafficInfoAverageVO getInfoAvg(TrafficInfoAverageDTO trafficInfoAverageDTO) {
        LocalDate beginDate = trafficInfoAverageDTO.getBeginDate();
        LocalDate endDate = trafficInfoAverageDTO.getEndDate();
        LocalTime beginTime;
        LocalTime endTime;
        if(beginDate==null){
            beginDate = LocalDate.of(1999,1,1);
        }
        if(endDate==null){
            endDate = LocalDate.of(5000,1,1);
        }
        if (trafficInfoAverageDTO.getBeginTime() != null) {
            beginTime = LocalTime.of(trafficInfoAverageDTO.getBeginTime(), 0, 0);
        }else{
            beginTime =  LocalTime.MIN;
        }
        if (trafficInfoAverageDTO.getEndTime() != null) {
            endTime = LocalTime.of(trafficInfoAverageDTO.getEndTime(), 0, 0);
        }else{
            endTime =  LocalTime.of(23, 59, 59);
        }

        return trafficInfoMapper.getInfoAvg(beginDate, endDate, beginTime, endTime);
    }
}
